<?php
namespace app\mapi\controller;
use app\mapi\model\MallShopsModel;

/**
 * 购物车控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class Shops extends Base{

    # 我的购物车
    public function carList(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');

            $FindUser = $this->checkLogin($Token);

            $MallShops = new MallShopsModel;

            $ListShops = $MallShops->mList(['shops_users'=>$FindUser['user_id']]);

            if(!empty($ListShops)){
                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无商品';
            }

            $this->ShowAjax($Code, $Msgs, $ListShops);
        }
    }

    # 加入购物车
    public function carAdd(){
        if(request()->isPost()){
            $Goods = input('post.goods_id','','intval');
            $Attrs = input('post.attrs_id','','intval');
            $Sizes = input('post.number','','intval')  ? input('post.number','','intval') : 1;
            $Token = input('post.token','','trim');

            if(empty($Goods)){
                $this->ShowAjax(2, '缺少goods_id参数');
            }

            if(empty($Attrs)){
                $this->ShowAjax(2, '缺少attrs_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $MallShops = new MallShopsModel;

            $FindShops = $MallShops->mInfo(['shops_goods'=>$Goods, 'shops_attrs'=>$Attrs, 'shops_users'=>$FindUser['user_id']]);

            if(empty($FindShops)){
                $Param = [];
                $Param['shops_goods'] = $Goods;
                $Param['shops_attrs'] = $Attrs;
                $Param['shops_sizes'] = $Sizes;
                $Param['shops_users'] = $FindUser['user_id'];

                $BackShops = $MallShops->mAdd($Param);
            }else{
                $Where = [];
                $Where['shops_goods'] = $Goods;
                $Where['shops_attrs'] = $Attrs;
                $Where['shops_users'] = $FindUser['user_id'];

                $Param = [];
                $Param['shops_sizes'] = $Sizes;

                $BackShops = $MallShops->mEdit($Where, $Param);
            }

            $this->ShowAjax($BackShops['code'], $BackShops['msgs']);
        }
    }

    # 删除购物车
    public function carDel(){
        if(request()->isPost()){
            $Goods = input('post.goods_id','','intval');
            $Attrs = input('post.attrs_id','','intval');
            $Token = input('post.token','','trim');

            if(empty($Goods)){
                $this->ShowAjax(2, '缺少goods_id参数');
            }

            if(empty($Attrs)){
                $this->ShowAjax(2, '缺少attrs_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $Where = [];
            $Where['shops_goods'] = $Goods;
            $Where['shops_attrs'] = $Attrs;
            $Where['shops_users'] = $FindUser['user_id'];

            $MallShops = new MallShopsModel;

            $BackShops = $MallShops->mDel($Where);

            $this->ShowAjax($BackShops['code'], $BackShops['msgs']);
        }
    }

}